/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elderberryni2.panels;

import elderberryni2.Main;
import elderberryni2.data.container.DataContainer;
import elderberryni2.interfaces.*;
import info.monitorenter.gui.chart.Chart2D;
import info.monitorenter.gui.chart.IAxis;
import info.monitorenter.gui.chart.IAxisLabelFormatter;
import info.monitorenter.gui.chart.ITrace2D;
import info.monitorenter.gui.chart.axis.AAxis;
import info.monitorenter.gui.chart.axis.AxisLinear;
import info.monitorenter.gui.chart.labelformatters.LabelFormatterNumber;
import info.monitorenter.gui.chart.traces.Trace2DLtd;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JCheckBox;
import javax.swing.JSlider;

/**
 *
 * @author warringer
 */
public class EnviroChartPanel extends javax.swing.JPanel implements IChartSavable,
        IDataCollecting {

    /**
     * Creates new form EnviroChartPanel
     */
    public EnviroChartPanel(Main parent) {
        this.parent = parent;
        initComponents();
        initChart();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        intervallSlider = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        intervallLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        timeChart = new Chart2D();
        altitudeChart = new Chart2D();
        saveButton = new javax.swing.JButton();
        saveAllButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        DynPresCheckBox = new javax.swing.JCheckBox();
        statPresCheckBox = new javax.swing.JCheckBox();
        jCheckBox1 = new javax.swing.JCheckBox();
        tempCheckBox = new javax.swing.JCheckBox();

        setPreferredSize(new java.awt.Dimension(765, 520));

        intervallSlider.setMajorTickSpacing(50);
        intervallSlider.setMaximum(600);
        intervallSlider.setMinimum(10);
        intervallSlider.setMinorTickSpacing(10);
        intervallSlider.setPaintTicks(true);
        intervallSlider.setSnapToTicks(true);
        intervallSlider.setValue(30);
        intervallSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                intervallSliderStateChanged(evt);
            }
        });

        jLabel1.setText("Displayed Time Intervall:");

        intervallLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, intervallSlider, org.jdesktop.beansbinding.ELProperty.create("${value}"), intervallLabel, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel2.setText("s");

        javax.swing.GroupLayout timeChartLayout = new javax.swing.GroupLayout(timeChart);
        timeChart.setLayout(timeChartLayout);
        timeChartLayout.setHorizontalGroup(
            timeChartLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 736, Short.MAX_VALUE)
        );
        timeChartLayout.setVerticalGroup(
            timeChartLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 407, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Time Chart", timeChart);

        javax.swing.GroupLayout altitudeChartLayout = new javax.swing.GroupLayout(altitudeChart);
        altitudeChart.setLayout(altitudeChartLayout);
        altitudeChartLayout.setHorizontalGroup(
            altitudeChartLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 736, Short.MAX_VALUE)
        );
        altitudeChartLayout.setVerticalGroup(
            altitudeChartLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 407, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Altitude Chart", altitudeChart);

        saveButton.setText("Save Charts");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        saveAllButton.setText("Save All");
        saveAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveAllButtonActionPerformed(evt);
            }
        });

        jLabel3.setText("Show Data of:");

        DynPresCheckBox.setSelected(true);
        DynPresCheckBox.setText("Dynamic Pressure");
        DynPresCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DynPresCheckBoxActionPerformed(evt);
            }
        });

        statPresCheckBox.setSelected(true);
        statPresCheckBox.setText("Static Pressure");
        statPresCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                statPresCheckBoxActionPerformed(evt);
            }
        });

        jCheckBox1.setSelected(true);
        jCheckBox1.setText("Atmospheric Density");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        tempCheckBox.setSelected(true);
        tempCheckBox.setText("Temperature");
        tempCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tempCheckBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(intervallLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2))
                            .addComponent(intervallSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(statPresCheckBox)
                                        .addGap(18, 18, 18)
                                        .addComponent(tempCheckBox))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(DynPresCheckBox)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jCheckBox1)))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(saveButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(saveAllButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 434, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(intervallLabel)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(intervallSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(26, 26, 26))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(DynPresCheckBox)
                                    .addComponent(jCheckBox1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(statPresCheckBox)
                                    .addComponent(tempCheckBox))
                                .addContainerGap())))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(saveAllButton)
                        .addContainerGap())))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        saveChart();
    }//GEN-LAST:event_saveButtonActionPerformed

    private void saveAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAllButtonActionPerformed
        parent.saveAllCharts();
    }//GEN-LAST:event_saveAllButtonActionPerformed

    private void intervallSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_intervallSliderStateChanged
        JSlider source = (JSlider) evt.getSource();
        if (!source.getValueIsAdjusting()) {
            int size = (int) source.getValue() * 2;    
            changeSize(size);
            duration = size;
        }
    }//GEN-LAST:event_intervallSliderStateChanged

    private void DynPresCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DynPresCheckBoxActionPerformed
        JCheckBox source = (JCheckBox) evt.getSource();
        if (source.isSelected()) {
            toggleTrace("DYN", true);
        } else {
            toggleTrace("DYN", false);
        }
    }//GEN-LAST:event_DynPresCheckBoxActionPerformed

    private void statPresCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_statPresCheckBoxActionPerformed
        JCheckBox source = (JCheckBox) evt.getSource();
        if (source.isSelected()) {
            toggleTrace("STAT", true);
        } else {
            toggleTrace("STAT", false);
        }
    }//GEN-LAST:event_statPresCheckBoxActionPerformed

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        JCheckBox source = (JCheckBox) evt.getSource();
        if (source.isSelected()) {
            toggleTrace("RHO", true);
        } else {
            toggleTrace("RHO", false);
        }
    }//GEN-LAST:event_jCheckBox1ActionPerformed

    private void tempCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tempCheckBoxActionPerformed
        JCheckBox source = (JCheckBox) evt.getSource();
        if (source.isSelected()) {
            toggleTrace("TEMP", true);
        } else {
            toggleTrace("TEMP", false);
        }
    }//GEN-LAST:event_tempCheckBoxActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox DynPresCheckBox;
    private Chart2D altitudeChart;
    private javax.swing.JLabel intervallLabel;
    private javax.swing.JSlider intervallSlider;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JButton saveAllButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JCheckBox statPresCheckBox;
    private javax.swing.JCheckBox tempCheckBox;
    private Chart2D timeChart;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private HashMap<String, ITrace2D> tracesTime = null;
    private HashMap<String, ITrace2D> tracesAlt = null;
    private int savesPerSecond = 2;
    private double lastTime = 0;
    private int duration = 30 * savesPerSecond;
    private IAxis time = null;
    private IAxis alt = null;
    private IAxis tempTime, tempAlt = null;
    private IAxis presTime, presAlt = null;
    private IAxis rhoTime, rhoAlt = null;
    
    private Main parent = null;
    
    private String saveFolder = "";

    private void initChart() {
        altitudeChart.getAxisX().setPaintGrid(true);
        altitudeChart.getAxisY().setPaintGrid(true);
        altitudeChart.getAxisX().setAxisTitle(new IAxis.AxisTitle("Altitude in m"));
        altitudeChart.getAxisY().setAxisTitle(new IAxis.AxisTitle("Pressure in bar"));
        alt = altitudeChart.getAxisX();
        presAlt = altitudeChart.getAxisY();
        tempAlt = new AxisLinear();
        tempAlt.setAxisTitle(new IAxis.AxisTitle("Temperature in °C"));
        altitudeChart.addAxisYLeft((AAxis) tempAlt);
        rhoAlt = new AxisLinear();
        rhoAlt.setAxisTitle(new IAxis.AxisTitle("Density in kg/m³"));
        altitudeChart.addAxisYRight((AAxis) rhoAlt);
        
        timeChart.getAxisX().setPaintGrid(true);
        timeChart.getAxisY().setPaintGrid(true);
        timeChart.getAxisX().setAxisTitle(new IAxis.AxisTitle("Time in s"));
        timeChart.getAxisY().setAxisTitle(new IAxis.AxisTitle("Pressure in bar"));
        time = timeChart.getAxisX();
        presTime = timeChart.getAxisY();
        tempTime = new AxisLinear();
        tempTime.setAxisTitle(new IAxis.AxisTitle("Temperature in °C"));
        timeChart.addAxisYLeft((AAxis) tempTime);
        rhoTime = new AxisLinear();
        rhoTime.setAxisTitle(new IAxis.AxisTitle("Density in kg/m³"));
        timeChart.addAxisYRight((AAxis) rhoTime);
        
        IAxisLabelFormatter format = new LabelFormatterNumber(new DecimalFormat("###E00"));
        presTime.setFormatter(format);
        presAlt.setFormatter(format);
        rhoTime.setFormatter(format);
        rhoAlt.setFormatter(format);
        alt.setFormatter(format);
        
        presTime.getAxisTitle().setTitleColor(Color.red);
        rhoTime.getAxisTitle().setTitleColor(Color.blue);
        tempTime.getAxisTitle().setTitleColor(Color.magenta);
        
        presAlt.getAxisTitle().setTitleColor(Color.red);
        rhoAlt.getAxisTitle().setTitleColor(Color.blue);
        tempAlt.getAxisTitle().setTitleColor(Color.magenta);
        
        tracesTime = new HashMap<String, ITrace2D>();
        tracesAlt = new HashMap<String, ITrace2D>();
    }
    
    public void newDataPoint(DataContainer dataPoint) {
        String uuid = dataPoint.getUuid();
        double timestamp = new Double(dataPoint.getTimestamp());
        double altitude = dataPoint.getLocation().getAltitude();
        double difference = Math.abs(lastTime - timestamp);
        if (difference > (1f / savesPerSecond)) {
            lastTime = timestamp;
            ITrace2D traceDynPresAlt = null;
            ITrace2D traceStatPresAlt = null;
            ITrace2D traceRhoAlt = null;
            ITrace2D traceTempAlt = null;
            
            ITrace2D traceDynPresTime = null;
            ITrace2D traceStatPresTime = null;
            ITrace2D traceRhoTime = null;
            ITrace2D traceTempTime = null;
            
            if (tracesAlt.isEmpty() || !tracesAlt.containsKey(uuid+"DYNalt")) {
                traceDynPresAlt = new Trace2DLtd(duration, uuid + " Dynamic Pressure");
                traceStatPresAlt = new Trace2DLtd(duration, uuid + " Static Pressure");
                traceRhoAlt = new Trace2DLtd(duration, uuid + " Density");
                traceTempAlt = new Trace2DLtd(duration, uuid + " Temperature");
                
                traceDynPresAlt.setColor(Color.red);
                traceStatPresAlt.setColor(Color.green);
                traceRhoAlt.setColor(Color.blue);
                traceTempAlt.setColor(Color.MAGENTA);
                
                altitudeChart.addTrace(traceDynPresAlt, alt, presAlt);
                altitudeChart.addTrace(traceStatPresAlt, alt, presAlt);
                altitudeChart.addTrace(traceRhoAlt, alt, rhoAlt);
                altitudeChart.addTrace(traceTempAlt, alt, tempAlt);
                
                tracesAlt.put(uuid+"DYNalt", traceDynPresAlt);
                tracesAlt.put(uuid+"STATalt", traceStatPresAlt);
                tracesAlt.put(uuid+"RHOalt", traceRhoAlt);
                tracesAlt.put(uuid+"TEMPalt", traceTempAlt);
            } else {
                traceDynPresAlt = tracesAlt.get(uuid+"DYNalt");
                traceStatPresAlt = tracesAlt.get(uuid+"STATalt");
                traceRhoAlt = tracesAlt.get(uuid+"RHOalt");
                traceTempAlt = tracesAlt.get(uuid+"TEMPalt");
            }
            if (tracesTime.isEmpty() || !tracesTime.containsKey(uuid+"DYNtime")) {
                traceDynPresTime = new Trace2DLtd(duration, uuid + " Dynamic Pressure");
                traceStatPresTime = new Trace2DLtd(duration, uuid + " Static Pressure");
                traceRhoTime = new Trace2DLtd(duration, uuid + " Density");
                traceTempTime = new Trace2DLtd(duration, uuid + " Temperature");
                
                traceDynPresTime.setColor(Color.red);
                traceStatPresTime.setColor(Color.green);
                traceRhoTime.setColor(Color.blue);
                traceTempTime.setColor(Color.MAGENTA);
                
                timeChart.addTrace(traceDynPresTime, time, presTime);
                timeChart.addTrace(traceStatPresTime, time, presTime);
                timeChart.addTrace(traceRhoTime, time, rhoTime);
                timeChart.addTrace(traceTempTime, time, tempTime);
                
                tracesTime.put(uuid+"DYNtime", traceDynPresTime);
                tracesTime.put(uuid+"STATtime", traceStatPresTime);
                tracesTime.put(uuid+"RHOtime", traceRhoTime);
                tracesTime.put(uuid+"TEMPtime", traceTempTime);
            } else {
                traceDynPresTime = tracesTime.get(uuid+"DYNtime");
                traceStatPresTime = tracesTime.get(uuid+"STATtime");
                traceRhoTime = tracesTime.get(uuid+"RHOtime");
                traceTempTime = tracesTime.get(uuid+"TEMPtime");
            }
            traceDynPresAlt.addPoint(altitude, dataPoint.getEnviro().getDynP());
            traceStatPresAlt.addPoint(altitude, dataPoint.getEnviro().getStatP());
            traceRhoAlt.addPoint(altitude, dataPoint.getEnviro().getRho());
            traceTempAlt.addPoint(altitude, dataPoint.getEnviro().getTemperature());
            
            traceDynPresTime.addPoint(timestamp, dataPoint.getEnviro().getDynP());
            traceStatPresTime.addPoint(timestamp, dataPoint.getEnviro().getStatP());
            traceRhoTime.addPoint(timestamp, dataPoint.getEnviro().getRho());
            traceTempTime.addPoint(timestamp, dataPoint.getEnviro().getTemperature());
        }
    }
    
    public void changeSize(int size) {
        Iterator<Map.Entry<String, ITrace2D>> iterator = tracesTime.entrySet().iterator();
        while (iterator.hasNext()) {
            Trace2DLtd trace = (Trace2DLtd) iterator.next().getValue();
            trace.setMaxSize(size);
        }
        iterator = tracesAlt.entrySet().iterator();
        while (iterator.hasNext()) {
            Trace2DLtd trace = (Trace2DLtd) iterator.next().getValue();
            trace.setMaxSize(size);
        }
    }

    public void setSavesPerSecond(int savesPerSecond) {
        this.savesPerSecond = savesPerSecond;
    }

    public void setSaveFolder(String saveFolder) {
        this.saveFolder = saveFolder;
    }
    
    @Override
    public void saveChart() {
        try {
            BufferedImage img = timeChart.snapShot(1024, 768);
            File out = new File(this.saveFolder+"time.png");
            ImageIO.write(img, "png", out);
            img = altitudeChart.snapShot(1024, 768);
            File out2 = new File(this.saveFolder+"altitude.png");
            ImageIO.write(img, "png", out2);
        } catch (IOException ex) {
            Logger.getLogger(MainChartPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void toggleTrace(String name, boolean b) {
        toggleTracePerChart(timeChart, tracesTime, name+"time", b);
        toggleTracePerChart(altitudeChart, tracesAlt, name+"alt", b);
    }

    private void toggleTracePerChart(Chart2D chart, HashMap<String, ITrace2D> traces, String name, boolean b) {
        Set<String> keys = traces.keySet();
        ArrayList<String> toggleKeys = new ArrayList<String>();
        for (String key : keys) {
            if (key.contains(name)) {
                toggleKeys.add(key);
            }
        }
        for (String key : toggleKeys) {
            ITrace2D trace = traces.get(key);
            if (b) {
                if (key.contains("DYNtime") || key.contains("STATtime")) {
                    chart.addTrace(trace, time, presTime);
                } else if (key.contains("DYNalt") || key.contains("STATalt")) {
                    chart.addTrace(trace, alt, presAlt);
                } else if (key.contains("RHOtime")) {
                    chart.addTrace(trace, time, rhoTime);
                } else if (key.contains("RHOalt")) {
                    chart.addTrace(trace, alt, rhoAlt);
                } else if (key.contains("TEMPtime")) {
                    chart.addTrace(trace, time, tempTime);
                } else if (key.contains("TEMPalt")) {
                    chart.addTrace(trace, alt, tempAlt);
                }
            } else {
                chart.removeTrace(trace);
            }
        }
    }
    
    public void clear() {
        tracesAlt.clear();
        tracesTime.clear();
    }
}
